package ktarou.m1k4.icu.util;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.tags.FormTag;
import org.htmlparser.tags.InputTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

public class HTMLUtil {
	private List<String> catList = new ArrayList<String>();
	private String[] uploadIdentifier = { "", "", "", "" };
	private static HTMLUtil instance = null;
	private HTMLUtil(){
		
	}
	public static HTMLUtil getInstance(){
		if(instance == null){
			instance= new HTMLUtil();
		}
		return instance;
	}
	public List<String> getCatList() {
		return catList;
	}

	public void setCatList(List<String> catList) {
		this.catList = catList;
	}

	public void prosesHTML(String stringToParse) {
		Parser parser = null;
		Parser parser2 = null;
		parser = new Parser(new Lexer(stringToParse));
		parser2 = new Parser(new Lexer(stringToParse));
		NodeList nodeList = null;
		NodeList nodeList2 = null;
		try {
			nodeList = parser.extractAllNodesThatMatch(new TagNameFilter("input"));//parse(new TagNameFilter("input"));
			nodeList2 = parser2.extractAllNodesThatMatch(new TagNameFilter("form"));
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SimpleNodeIterator sni = nodeList.elements();
		while (sni.hasMoreNodes()) {
			InputTag nd = (InputTag) sni.nextNode();
			System.out.println("Match " + nd.getAttribute("name"));
			if (nd.getAttribute("name").equals("UPLOAD_IDENTIFIER")) {
				uploadIdentifier[0] = nd.getAttribute("value");
				System.out.println("Match " + nd.getAttribute("name"));
			}
			if (nd.getAttribute("name").equals("user_id")) {
				uploadIdentifier[1] = nd.getAttribute("value");
				System.out.println("Match " + nd.getAttribute("name"));
			}
			if (nd.getAttribute("name").equals("post_url")) {
				uploadIdentifier[2] = nd.getAttribute("value");
				System.out.println("Match " + nd.getAttribute("name"));
			}

		}
		FormTag nd = (FormTag) nodeList2.elementAt(0);
		uploadIdentifier[3] = nd.getAttribute("action");
	}
	public String[] getUploadIdentifier() {
		return uploadIdentifier;
	}
	public void setUploadIdentifier(String[] uploadIdentifier) {
		this.uploadIdentifier = uploadIdentifier;
	}
}
